package com.finger.forest;

import android.Manifest;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.finger.forest.base.BaseActivity;
import com.finger.forest.base.BaseApi;
import com.finger.forest.base.BaseView;
import com.finger.forest.base.CommonKey;
import com.finger.forest.base.ResponseTag;
import com.finger.forest.http.Constant;
import com.finger.forest.http.presenter.RegisterPresenter;
import com.finger.forest.model.GridUserModel;
import com.finger.forest.model.LoginModel;
import com.finger.forest.service.GpsService;
import com.finger.forest.utils.ClickUtils;
import com.finger.forest.utils.NetUtil;
import com.finger.forest.utils.StringUtil;
import com.finger.forest.utils.ToastUtils;
import com.finger.forest.utils.encode.PrefsUtil;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 登录
 */
public class LoginActivity extends BaseActivity<RegisterPresenter> implements BaseView {

    /**
     * 用户名
     */
    @BindView(R.id.userName)
    EditText userNameEt;

    /**
     * 密码
     */
    @BindView(R.id.password)
    EditText passwordEt;

    /**
     * 登录
     */
    @BindView(R.id.loginBtn)
    Button loginBtn;

    /**
     * 离线登录
     */
    @BindView(R.id.loginOffBtn)
    Button loginOffBtn;

    /**
     * 服务器地址
     */
    @BindView(R.id.tv_select_service)
    TextView selectServiceTv;

    /**
     * 记住密码
     */
    @BindView(R.id.check_box)
    CheckBox checkBox;

    @Override
    protected int setView() {
        return R.layout.activity_login;
    }

    @Override
    protected void receiveData() {

    }

    @Override
    protected void initializeView() {
        // 读取sharedpreference中的账号密码
        String userName = PrefsUtil.getString(LoginActivity.this, CommonKey.USER_NAME);
        String password = PrefsUtil.getString(LoginActivity.this, CommonKey.PASSWORD);
        userNameEt.setText(userName);
        passwordEt.setText(password);

        //判断网络状态，判断在线登录或离线登录
        if (NetUtil.isNetworkAvailable()) {
            loginBtn.setVisibility(View.VISIBLE);
            loginOffBtn.setVisibility(View.GONE);
        } else {
            loginBtn.setVisibility(View.GONE);
            loginOffBtn.setVisibility(View.VISIBLE);
        }

        int permission = ActivityCompat.checkSelfPermission(this,
                Manifest.permission.ACCESS_FINE_LOCATION);
        if (permission != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                    100);
        }
    }

    @Override
    protected void loadDataFromServer() {

    }

    @Override
    public void onSuccess(Object model, int tag, String msg) {

        switch (tag) {
            //用户登录
            case ResponseTag.USER_LOGIN:
                GridUserModel loginModel = (GridUserModel) model;
                //保存用户id/token/areaId
                PrefsUtil.setString(this, CommonKey.USER_ID, String.valueOf(loginModel.getId()));
                PrefsUtil.setString(this, CommonKey.TOKEN, loginModel.getToken());
                PrefsUtil.setString(this, CommonKey.AREA_ID, String.valueOf(loginModel.getResponsibleZoneId()));
                //保存在线登录状态
                PrefsUtil.setInt(this, CommonKey.ISONLINE, 1);

                openActivity(MainActivity.class);
                ToastUtils.show(msg == null ? " " : msg);
                //登录成功是否记住密码
                if (checkBox.isChecked()) {
                    PrefsUtil.setString(LoginActivity.this, CommonKey.USER_NAME, userNameEt.getText().toString());
                    PrefsUtil.setString(LoginActivity.this, CommonKey.PASSWORD, passwordEt.getText().toString());
                }
                finish();
                break;
            default:
                break;

        }
        hidePd();
    }

    @Override
    public void onError(int errcode, int tag, String message) {

    }

    @OnClick({R.id.tv_select_service, R.id.loginBtn, R.id.loginOffBtn})
    public void onClick(View v) {

        if (ClickUtils.isFastClick()) { //防止重复点击
            switch (v.getId()) {

                //服务器选择
                case R.id.tv_select_service:
                    Intent intent = new Intent(LoginActivity.this, SelectServiceActivity.class);
                    startActivity(intent);
                    break;

                //登录
                case R.id.loginBtn:
                    //如果网络不正常，则离线登录
                    if (!NetUtil.isNetworkAvailable()) {
                        loginOffBtn.setVisibility(View.VISIBLE);
                        ToastUtils.show(LoginActivity.this, "无网络，可选择离线登录");
                        return;
                    }
                    String userName = userNameEt.getText().toString();
                    String password = passwordEt.getText().toString();
                    if (StringUtil.isBlank(userName)) {
                        ToastUtils.show(LoginActivity.this, "用户名不能为空");
                        return;
                    }

                    if (StringUtil.isBlank(password)) {
                        ToastUtils.show(LoginActivity.this, "密码不能为空");
                        return;
                    }
                    showPd();
                    //登录请求
                    presenter.userLogin(userName, password, "1");
                    break;

                //离线登录
                case R.id.loginOffBtn:
                    //保存离线登录状态
                    PrefsUtil.setInt(this, CommonKey.ISONLINE, 0);
                    openActivity(MainActivity.class);
                    break;

                default:
                    break;
            }
        }
    }
}
